﻿using AutofacProject.Entity;
using AutofacProject.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutofacProject.Repository
{
    public class StudentRepository : IStudentRepository
    {
        private readonly AutofacDbContext _db;

        public StudentRepository(AutofacDbContext db)
        {
            _db = db;
        }
        public IQueryable<Student> GetList()
        {
            try
            {
                return _db.Student;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool Delete(Student student)
        {
            try
            {

                _db.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                //记录异常日志
                return false;
            }
        }
        public Student GetModel(int Id)
        {
            try
            {
                Student student = _db.Student.Find(Id);
                return student;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

        public int Insert(Student student)
        {
            try
            {
                _db.Entry(student).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return student.id;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

        public bool Update(Student student)
        {
            try
            {

                _db.Entry(student).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
